<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui">

    <ui:composition template="/template.xhtml">

        <ui:define name="title">
            <h:outputText value="#{bundle.CreateContratoTitle}"></h:outputText>
        </ui:define>

        <ui:define name="body">

            <h:panelGroup id="messagePanel" layout="block">
                <p:growl />
            </h:panelGroup>

            <h:form id="pgContratoCreate">

                <p:panelGrid id="pgContratoCreate" columns="4">

                    <h:outputLabel value="#{bundle.CreateContratoLabel_dataContrato}" for="dataContrato" />
                    <p:calendar id="dataContrato" value="#{contratoController.selected.dataContrato}" 
                                navigator="true" required="true" 
                                requiredMessage="#{bundle.CreateContratoRequiredMessage_dataContrato}"
                                pattern="dd/MM/yyyy" locale="pt" />

                    <h:outputLabel value="#{bundle.CreateContratoLabel_perDesconto}" for="perDesconto" />
                    <p:spinner id="perDesconto" value="#{contratoController.selected.perDesconto}" 
                               title="#{bundle.CreateContratoTitle_perDesconto}" required="true" 
                               requiredMessage="#{bundle.CreateContratoRequiredMessage_perDesconto}"
                               sufix="%" stepFactor="0.1" />

                    <h:outputLabel value="#{bundle.CreateContratoLabel_observacao}" for="observacao" />
                    <p:inputTextarea rows="4" cols="30" id="observacao" value="#{contratoController.selected.observacao}" 
                                     title="#{bundle.CreateContratoTitle_observacao}" required="true" 
                                     requiredMessage="#{bundle.CreateContratoRequiredMessage_observacao}"/>

                    <h:outputLabel value="#{bundle.CreateContratoLabel_responsavel}" for="responsavel" />
                    <p:selectOneMenu id="responsavel" value="#{contratoController.selected.responsavel}" 
                                     label="#{bundle.CreateContratoTitle_responsavel}" required="true" 
                                     requiredMessage="#{bundle.CreateContratoRequiredMessage_responsavel}">
                        <f:selectItems value="#{funcionarioController.itemsAvailableSelectOne}"/>
                    </p:selectOneMenu>

                    <h:outputLabel value="#{bundle.CreateContratoLabel_cliente}" for="cliente" />
                    <p:selectOneMenu id="cliente" value="#{contratoController.selected.cliente}" 
                                     label="#{bundle.CreateContratoTitle_cliente}" required="true" 
                                     requiredMessage="#{bundle.CreateContratoRequiredMessage_cliente}">
                        <f:selectItems value="#{clienteController.itemsAvailableSelectOne}"/>
                    </p:selectOneMenu>

                    <p:commandButton action="#{contratoController.mostrarMatricula}"
                                     update=":formContratoMatriculaList:tableContratoMatriculaList"
                                     rendered="#{(!contratoController.renderizaMatricula)}"
                                     value="Inserir Matrículas" 
                                     icon="ui-icon-pencil" ajax="false"/>

                    <p:commandButton action="#{contratoController.create}" value="#{bundle.CreateContratoSaveLink}" 
                                     rendered="#{contratoController.renderizaBotaoSalvar}" 
                                     icon="ui-icon-disk" ajax="false"/>
                    <p:commandButton value="Limpar" action="#{contratoController.prepareCreate}"
                                     icon="ui-icon-document-b" ajax="false" />

                </p:panelGrid>

                <br/>
                <br/>

            </h:form>

            <h:form id="formContratoMatriculaList">

                <h:panelGroup>

                    <p:dataTable id="tableContratoMatricula" value="#{contratoController.itemsMatricula}" var="item"
                                 style="border:solid 1px" emptyMessage="#{bundle.ListMatriculaEmpty}" 
                                 rendered="#{contratoController.renderizaMatricula}">

                        <f:facet name="header">#{bundle.ListMatriculaTitle}</f:facet>

                        <p:column>
                            <f:facet name="header">#{bundle.ListMatriculaTitle_dataInicio}</f:facet>
                            <p:calendar value="#{item.dataInicio}" pattern="dd/MM/yyyy"
                                        locale="pt" disabled="true" />
                        </p:column>

                        <p:column>
                            <f:facet name="header">#{bundle.ListMatriculaTitle_dataFim}</f:facet>
                            <p:calendar value="#{item.dataFim}" pattern="dd/MM/yyyy"
                                        locale="pt" disabled="true" />
                        </p:column>

                        <p:column>
                            <f:facet name="header">#{bundle.ListMatriculaTitle_situacao}</f:facet>
                            <h:outputText value="#{item.situacao.toString()}" />
                        </p:column>

                        <p:column>
                            <f:facet name="header">#{bundle.ListMatriculaTitle_aluno}</f:facet>
                            <h:outputText value="#{item.aluno.nome}" />
                        </p:column>

                        <p:column>
                            <f:facet name="header">#{bundle.ListMatriculaTitle_turma}</f:facet>
                            <h:outputText value="#{item.turma.toString()}" />
                        </p:column>

                        <p:column>
                            <f:facet name="header">#{bundle.ListProdutoTitle_valor}</f:facet>
                            <h:outputText value="R$ #{item.turma.modalidade.valor}" />
                            <f:facet name="footer">
                                <h:outputText value="Valor Total da Contrato " />
                            </f:facet>
                        </p:column>

                        <p:column>
                            <f:facet name="header">#{bundle.ListProdutoTitle_valorTotal}</f:facet>
                            <h:outputText value="R$ #{item.valorTotal}" />
                            <f:facet name="footer">
                                <h:outputText value="R$ #{contratoController.valorTotalContrato}" />
                            </f:facet>
                        </p:column>

                        <p:column style="alignment-baseline: central" 
                                  rendered="#{!contratoController.renderizaCondicoesContrato}">
                            <f:facet name="header">
                                <p:commandButton actionListener="#{contratoController.prepareCreateMatricula}" 
                                                 value="#{bundle.ListMatriculaCreateLink}"
                                                 update=":formContratoMatriculaCreate:pgMatriculaCreate"
                                                 oncomplete="dialogMatriculaCreate.show()" />
                            </f:facet>

                            <p:commandButton actionListener="#{contratoController.prepareEditMatricula}" 
                                             value="#{bundle.ListMatriculaEditLink}"
                                             update=":formContratoMatriculaEdit:pgMatriculaEdit"
                                             oncomplete="dialogMatriculaEdit.show()" 
                                             ajax="false"/>

                            <p:commandButton actionListener="#{contratoController.destroyMatricula}" 
                                             value="#{bundle.ListMatriculaDestroyLink}" ajax="false"/>

                        </p:column>

                        <f:facet name="footer">
                            <p:commandButton value="Definir Pagamentos"
                                             style="alignment-baseline: central"
                                             action="#{contratoController.mostrarCondicoesContrato}"
                                             update=":formContratoCondicoesContratoList:tableContratoCondicoesContrato"
                                             rendered="#{(!contratoController.renderizaCondicoesContrato)}"
                                             ajax="false"/>
                        </f:facet>

                    </p:dataTable>
                </h:panelGroup>

                <br/>
                <br/>

            </h:form>

            <h:form id="formContratoCondicoesContratoList">

                <h:panelGroup>
                    <p:dataTable id="tableContratoCondicoesContrato" value="#{contratoController.itemsCondicoesContrato}" 
                                 var="item" style="border:solid 1px" 
                                 emptyMessage="#{bundle.ListCondicoesContratoEmpty}" 
                                 rendered="#{contratoController.renderizaCondicoesContrato}">

                        <f:facet name="header">#{bundle.ListCondicoesContratoTitle}</f:facet>

                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListCondicoesContratoTitle_condicaopagamento}"/>
                            </f:facet>
                            <h:outputText value="#{item.condicaoPagamento.toString()}"/>
                            <f:facet name="footer">
                                <h:outputText value="Saldo a Pagar " />
                            </f:facet>
                        </p:column>

                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListCondicoesContratoTitle_condicaopagamento}"/>
                            </f:facet>
                            <h:outputText value="#{item.valor}"/>
                            <f:facet name="footer">
                                <h:outputText 
                                    value="R$ #{(contratoController.valorTotalContrato - contratoController.valorTotalCondicoesContrato)}" />
                            </f:facet>
                        </p:column>

                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListCondicoesContratoTitle_observacao}"/>
                            </f:facet>
                            <h:outputText value="#{item.observacao}"/>
                            <f:facet name="footer">
                                <h:outputText value="Soma dos Pagamentos " />
                            </f:facet>
                        </p:column>

                        <p:column>
                            <f:facet name="header">
                                <p:commandButton actionListener="#{contratoController.prepareCreateCondicoesContrato}" 
                                                 value="#{bundle.ListCondicoesContratoCreateLink}"
                                                 rendered="#{(!contratoController.renderizaBotaoSalvar)}"
                                                 update=":formContratoCondicoesContratoCreate:pgCondicoesContratoCreate"
                                                 oncomplete="dialogCondicoesContratoCreate.show()" />
                            </f:facet>

                            <p:commandButton action="#{contratoController.prepareEditCondicoesContrato}" 
                                             value="#{bundle.ListCondicoesContratoEditLink}"
                                             update=":formContratoCondicoesContratoEdit:pgCondicoesContratoEdit"
                                             oncomplete="dialogCondicoesContratoEdit.show()" 
                                             ajax="false"/>

                            <p:commandButton actionListener="#{contratoController.destroyCondicoesContrato}" 
                                             value="#{bundle.ListCondicoesContratoDestroyLink}" ajax="false"/>

                            <f:facet name="footer">
                                <h:outputText value="R$ #{contratoController.valorTotalCondicoesContrato}" />
                            </f:facet>

                        </p:column>

                    </p:dataTable>
                </h:panelGroup>

            </h:form>

            <h:form id="formContratoMatriculaCreate">

                <p:dialog header="#{bundle.CreateMatriculaTitle}" widgetVar="dialogMatriculaCreate" 
                          resizable="false" modal="true" showEffect="slide">
                    <p:panelGrid id="pgMatriculaCreate" columns="2">

                        <h:outputLabel value="#{bundle.CreateMatriculaLabel_aluno}" for="aluno" />
                        <p:selectOneMenu id="aluno" value="#{contratoController.matriculaSelected.aluno}" 
                                         label="#{bundle.CreateMatriculaTitle_aluno}" required="true" 
                                         requiredMessage="#{bundle.CreateMatriculaRequiredMessage_aluno}">
                            <f:selectItems value="#{alunoController.itemsAvailableSelectOne}"/>
                        </p:selectOneMenu>

                        <h:outputLabel value="#{bundle.CreateMatriculaLabel_dataInicio}" for="dataInicio" />
                        <p:calendar id="dataInicio" value="#{contratoController.matriculaSelected.dataInicio}" 
                                     title="#{bundle.CreateMatriculaTitle_dataInicio}" required="true" 
                                     requiredMessage="#{bundle.CreateMatriculaRequiredMessage_dataInicio}"
                                     pattern="dd/MM/yyyy" locale="pt" >
                            <p:ajax event="blur" listener="#{contratoController.gatilhaDataIni}" />
                        </p:calendar>

                        <h:outputLabel value="#{bundle.CreateMatriculaLabel_meses}" for="meses" />
                        <p:spinner id="meses" value="#{contratoController.matriculaSelected.meses}" 
                                   title="#{bundle.CreateMatriculaTitle_meses}" min="#{1}">
                            <p:ajax event="blur" update="dataFim" listener="#{contratoController.gatilhaDataFim}" />
                        </p:spinner>

                        <h:outputLabel value="#{bundle.CreateMatriculaLabel_dataFim}" for="dataFim" />
                        <p:calendar id="dataFim" value="#{contratoController.matriculaSelected.dataFim}" 
                                     title="#{bundle.CreateMatriculaTitle_dataFim}" required="true" 
                                     requiredMessage="#{bundle.CreateMatriculaRequiredMessage_dataFim}"
                                     locale="pt" disabled="true" pattern="dd/MM/yyyy" >
                        </p:calendar>

                        <h:outputLabel value="#{bundle.CreateMatriculaLabel_situacao}" for="situacao" />
                        <p:selectOneMenu id="situacao" value="#{contratoController.matriculaSelected.situacao}" 
                                         label="#{bundle.CreateMatriculaTitle_situacao}" required="true" 
                                         requiredMessage="#{bundle.CreateMatriculaRequiredMessage_situacao}">
                            <f:selectItems value="#{situacaoController.itemsAvailableSelectOne}"/>
                        </p:selectOneMenu>

                        <h:outputLabel value="#{bundle.CreateMatriculaLabel_turma}" for="turma" />
                        <p:selectOneMenu id="turma" value="#{contratoController.matriculaSelected.turma}" 
                                         label="#{bundle.CreateMatriculaTitle_turma}" required="true" 
                                         requiredMessage="#{bundle.CreateMatriculaRequiredMessage_turma}">
                            <f:selectItems value="#{turmaController.itemsAvailableSelectOne}"/>
                        </p:selectOneMenu>

                        <p:commandButton actionListener="#{contratoController.createMatricula}" 
                                         value="#{bundle.CreateMatriculaSaveLink}"
                                         update=":formContratoCreate:pgContratoCreate"
                                         oncomplete="dialogMatriculaCreate.hide()"
                                         ajax="false" />
                        <p:commandButton value="Limpar" type="reset" ajax="false"/>
                    </p:panelGrid>

                </p:dialog>

            </h:form>

            <h:form id="formContratoMatriculaEdit">

                <p:dialog header="#{bundle.EditMatriculaTitle}" widgetVar="dialogMatriculaEdit" 
                          resizable="false" modal="true" showEffect="slide">
                    <p:panelGrid id="pgMatriculaEdit" columns="2">

                        <h:outputLabel value="#{bundle.EditMatriculaLabel_aluno}" for="aluno" />
                        <p:selectOneMenu id="aluno" value="#{contratoController.matriculaSelected.aluno}" 
                                         label="#{bundle.EditMatriculaTitle_aluno}" required="true" 
                                         requiredMessage="#{bundle.EditMatriculaRequiredMessage_aluno}">
                            <f:selectItems value="#{alunoController.itemsAvailableSelectOne}"/>
                        </p:selectOneMenu>

                        <h:outputLabel value="#{bundle.EditMatriculaLabel_dataInicio}" for="dataInicio" />
                        <p:calendar id="dataInicio" value="#{contratoController.matriculaSelected.dataInicio}" 
                                    title="#{bundle.EditMatriculaTitle_dataInicio}" required="true" 
                                    requiredMessage="#{bundle.EditMatriculaRequiredMessage_dataInicio}"
                                    locale="pt" pattern="dd/MM/yyyy" >
                        </p:calendar>

                        <h:outputLabel value="#{bundle.CreateMatriculaLabel_meses}" for="meses" />
                        <p:spinner id="meses" value="#{contratoController.matriculaSelected.meses}" 
                                   title="#{bundle.CreateMatriculaTitle_meses}" min="#{1}">
                            <p:ajax event="change" update="dataFim" listener="#{contratoController.gatilhaDataFim}" />
                        </p:spinner>

                        <h:outputLabel value="#{bundle.EditMatriculaLabel_dataFim}" for="dataFim" />
                        <p:calendar id="dataFim" value="#{contratoController.matriculaSelected.dataFim}" 
                                     title="#{bundle.EditMatriculaTitle_dataFim}" required="true" 
                                     requiredMessage="#{bundle.EditMatriculaRequiredMessage_dataFim}"
                                     locale="pt" pattern="99/99/9999" disabled="true" >
                            <f:convertDateTime pattern="dd/MM/yyyy" />
                        </p:calendar>

                        <h:outputLabel value="#{bundle.EditMatriculaLabel_situacao}" for="situacao" />
                        <p:selectOneMenu id="situacao" value="#{contratoController.matriculaSelected.situacao}" 
                                         label="#{bundle.EditMatriculaTitle_situacao}" required="true" 
                                         requiredMessage="#{bundle.EditMatriculaRequiredMessage_situacao}">
                            <f:selectItems value="#{situacaoController.itemsAvailableSelectOne}"/>
                        </p:selectOneMenu>

                        <h:outputLabel value="#{bundle.EditMatriculaLabel_turma}" for="turma" />
                        <p:selectOneMenu id="turma" value="#{contratoController.matriculaSelected.turma}" 
                                         label="#{bundle.EditMatriculaTitle_turma}" required="true" 
                                         requiredMessage="#{bundle.EditMatriculaRequiredMessage_turma}">
                            <f:selectItems value="#{turmaController.itemsAvailableSelectOne}"/>
                        </p:selectOneMenu>

                        <p:commandButton actionListener="#{contratoController.updateMatricula}" 
                                         value="#{bundle.EditMatriculaSaveLink}"
                                         update=":formContratoCreate:pgContratoCreate"
                                         oncomplete="dialogMatriculaEdit.hide()"
                                         ajax="false" />
                        <p:commandButton value="Limpar" type="reset" ajax="false"/>
                    </p:panelGrid>

                </p:dialog>

            </h:form>

            <h:form id="formContratoCondicoesContratoCreate">

                <p:dialog header="#{bundle.CreateCondicoesContratoTitle}" widgetVar="dialogCondicoesContratoCreate" 
                          resizable="false" modal="true" showEffect="slide">
                    <p:panelGrid id="pgCondicoesContratoCreate" columns="2">

                        <h:outputLabel value="#{bundle.CreateCondicoesContratoLabel_condicaopagamento}" 
                                       for="condicoesContratoCondicaoPagamentoCreate" />
                        <p:selectOneMenu id="condicoesContratoCondicaoPagamentoCreate" 
                                         value="#{contratoController.condicoesContratoSelected.condicaoPagamento}"
                                         label="#{bundle.CreateCondicoesContratoTitle_condicaopagamento}" required="true" 
                                         requiredMessage="#{bundle.CreateCondicoesContratoRequiredMessage_condicaopagamento}">
                            <f:selectItems value="#{condicaoPagamentoController.itemsAvailableSelectOne}"/>
                        </p:selectOneMenu>

                        <h:outputLabel value="#{bundle.CreateProdutoLabel_valor}" 
                                       for="condicoesContratoValorCreate" />
                        <p:inputText id="condicoesContratoValorCreate" value="#{contratoController.condicoesContratoSelected.valor}" 
                                     label="#{bundle.CreateProdutoTitle_valor}" required="true"
                                     requiredMessage="#{bundle.CreateProdutoRequiredMessage_valor}" />

                        <h:outputLabel value="#{bundle.CreateCondicoesContratoLabel_observacao}" 
                                       for="condicoesContratoObservacaoCreate" />
                        <p:inputTextarea id="condicoesContratoObservacaoCreate" 
                                         value="#{contratoController.condicoesContratoSelected.observacao}" 
                                         label="#{bundle.CreateCondicoesContratoTitle_observacao}" />

                        <p:commandButton actionListener="#{contratoController.createCondicoesContrato}" 
                                         value="#{bundle.CreateCondicoesContratoSaveLink}"
                                         update=":formContratoCreate:pgContratoCreate :formContratoMatriculaList:tableContratoMatricula"
                                         oncomplete="dialogCondicoesContratoCreate.hide()"
                                         ajax="false" />
                        <p:commandButton value="Limpar" type="reset" ajax="false"/>
                    </p:panelGrid>
                </p:dialog>

            </h:form>

            <h:form id="formContratoCondicoesContratoEdit">

                <p:dialog header="#{bundle.EditCondicoesContratoTitle}" widgetVar="dialogCondicoesContratoEdit" 
                          resizable="false" modal="true" showEffect="slide">
                    <p:panelGrid id="pgCondicoesContratoEdit" columns="2">

                        <h:outputLabel value="#{bundle.EditCondicoesContratoLabel_condicaopagamento}" 
                                       for="condicoesContratoCondicaoPagamentoEdit" />
                        <p:selectOneMenu id="condicoesContratoCondicaoPagamentoEdit" 
                                         value="#{contratoController.condicoesContratoSelected.condicaoPagamento}"
                                         label="#{bundle.EditCondicoesContratoTitle_condicaopagamento}" required="true" 
                                         requiredMessage="#{bundle.EditCondicoesContratoRequiredMessage_condicaopagamento}">
                            <f:selectItems value="#{condicaoPagamentoController.itemsAvailableSelectOne}"/>
                        </p:selectOneMenu>

                        <h:outputLabel value="#{bundle.EditProdutoLabel_valor}" 
                                       for="condicoesContratoValorEdit" />
                        <p:inputText id="condicoesContratoValorEdit" value="#{contratoController.condicoesContratoSelected.valor}" 
                                     label="#{bundle.EditProdutoTitle_valor}" required="true"
                                     requiredMessage="#{bundle.EditProdutoRequiredMessage_valor}" />

                        <h:outputLabel value="#{bundle.EditCondicoesContratoLabel_observacao}" 
                                       for="condicoesContratoObservacaoEdit" />
                        <p:inputTextarea id="condicoesContratoObservacaoEdit" 
                                         value="#{contratoController.condicoesContratoSelected.observacao}" 
                                         label="#{bundle.EditCondicoesContratoTitle_observacao}" />

                        <p:commandButton actionListener="#{contratoController.createCondicoesContrato}" 
                                         value="#{bundle.EditCondicoesContratoSaveLink}"
                                         update=":formContratoCreate:pgContratoCreate :formContratoMatriculaList:tableContratoMatricula"
                                         oncomplete="dialogCondicoesContratoEdit.hide()"
                                         ajax="false" />
                        <p:commandButton value="Limpar" type="reset" ajax="false"/>
                    </p:panelGrid>
                </p:dialog>

            </h:form>

        </ui:define>
    </ui:composition>

</html>
